<?php

namespace App\Http\Controllers;

use App\PostComment;
use Illuminate\Http\Request;

class PostCommentController extends Controller
{
    public function index(){
        $postComments = PostComment::latest('created_at')->Paginate(8);
        return view('sunPicture.postComment', compact('postComments'));
    }

    public function destroy($id)
    {
        $postComments = PostComment::find($id);
        
        $postComments->delete();

        session()->flash('success', '删除成功');
        return redirect()->route('postComments.index');
    }

    public function edit($id)
    {
        $postComments = PostComment::findOrFail($id);
        return view('sunPicture.postCommentEdit',compact('postComments'));
    }

    public function update(Request $request, $id)
    {
        $postComments = PostComment::findOrFail($id);

        $postComments->update([
            'sun_picture_id' => $request->get('sun_picture_id'),
            'comment_content' => $request->get('comment_content'),
            'user_id'=>$request->get('user_id')
        ]);
        $postComments->save();

        session()->flash('success', '更新成功');
        return redirect()->route('postComments.index');
    }


}
